﻿var options = { items_per_page: 20, link_to: "javascript:;", callback: pageChanged };
var account;
var nickname;
var realname;
var email;
var maxentries = 0;
$(function() {
    getValue();
    BindSearchSwitch();
    searchUsers();
    BindCheckBoxEvent($("#btn_delete"));
    $("#btn_search").click(function() {
        getValue();
        searchUsers();
    });

    $("#btn_delete").click(function() {

        confirmbox("确定要删除所有选中用户吗？");

        $(window.parent.document).find("#btn_sure").click(function() {
            closebox();

            var uids = "";
            $("input:checked").not("#cb_All").each(function() {
                uids += $(this).val() + ",";
            });

            $.ajax({
                type: "POST",
                url: "../api/user.ashx",
                data: { type: 'deleteuser', uids: uids, r: Math.random() },
                dataType: "html",
                success: function(data) {
                    if (data == 1) {
                        searchUsers();
                        facebox("操作已完成！");
                    }
                }
            });
        });
    });

});
function getValue() {
    account = $("#txt_account").val();
    nickname = $("#txt_nickname").val();
    realname = $("#txt_realname").val();
    email = $("#txt_email").val();
}
function searchUsers() {
    $.ajax({
        type: "GET",
        url: "../api/user.ashx",
        data: { type: 'usersnum', account: account, nickname: nickname, realname: realname, email: email, r: Math.random() },
        dataType: "html",
        success: function(data) {
            maxentries = data;
            if (data == 0) {
                $(".DG tbody").hide();
                $(".DG table").next().hide();
                $(".warning").show();
                return;
            }
            $("#pager").pagination(data, options);
            $(".DG tbody").show();
            $(".DG table").next().show();
            $(".warning").hide();
        }
    });
}
function pageChanged(index, jq) {
    $.ajax({
        type: "GET",
        url: "../api/user.ashx",
        data: { type: 'searchuser', account: account, nickname: nickname, realname: realname, email: email, pageindex: index + 1, pagesize: options.items_per_page, r: Math.random() },
        dataType: "json",
        success: function(data) {
            $(".DG tbody").empty();
            $.each(data, function(i, item) {
                $(".DG tbody")
                .append("<tr><td><input type='checkbox' value='" + item.uid + "' /></td>\
                            <td>" + item.row + "</td>\
                            <td>" + item.account + "</td>\
                            <td>" + item.realname + "</td>\
                            <td>" + item.nickname + "</td>\
                            <td>" + item.email + "</td>\
                            <td>\
                                <input type='button' class='editButton' />\
                                <input type='button' class='deleteButton' rel='" + item.uid + "' relname='" + item.realname + "' />\
                            </td>\
                        </tr>");
                $(".editButton:last").click(function() {
                    var location = rootpath + 'user/edituser.aspx?id=' + item.uid;
                    openDialog(location, "用户修改", 900, 560);
                    return false;
                });
                BindCheckBox($("#btn_delete"),$(":checkbox:last"));
            });
           
            bindDeleteEvent();
            $(".DG tbody tr").hover(function() { $(this).addClass("tdmover"); }, function() { $(this).removeClass("tdmover"); });
            formReady();
        }
    });
}
function bindDeleteEvent() {
    $(".deleteButton").click(function() {
        var uid = $(this).attr("rel");
        var uname = $(this).attr("relname");
        
        confirmbox("<div>确定要删除用户 <strong>" + uname + "</strong> 吗？</div>");

        $(window.parent.document).find("#btn_sure").click(function() {

            $.ajax({
                type: "POST",
                url: "../api/user.ashx",
                data: { type: 'deleteuser', uids: uid, r: Math.random() },
                dataType: "string",
                success: function(data) {
                    if (data == 1) {
                        closebox();
                        searchUsers();
                    }
                }
            });
        });
    });
}
